package dimension.stock;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Config {

    public static void saveDefaultConfig() {
        MainClass.INSTANCE.saveConfig();
        MainClass.INSTANCE.getConfig().addDefault("stockName","哈基ETF");
        MainClass.INSTANCE.getConfig().addDefault("tradeRate","0.0001");
        MainClass.INSTANCE.getConfig().addDefault("limitRate","0.3");
        MainClass.INSTANCE.getConfig().options().copyDefaults(true);
        MainClass.INSTANCE.saveConfig();
        MainClass.INSTANCE.reloadConfig();
    }

    public static String getStockName() {
        return MainClass.INSTANCE.getConfig().getString("stockName");
    }

    public static BigDecimal getTradeRate() {
        return new BigDecimal(MainClass.INSTANCE.getConfig().getString("tradeRate"));
    }

    public static void saveAvailableStock(String playerName,long count) {
        if (count<0L) {
            count = 0L;
        }
        MainClass.INSTANCE.getConfig().set("data." + playerName,count);
        MainClass.INSTANCE.saveConfig();
    }

    public static long getAvailableStock(String playerName) {
        MainClass.INSTANCE.getConfig().addDefault("data." +playerName,0L);
        return MainClass.INSTANCE.getConfig().getLong("data." + playerName);
    }

    public static BigDecimal getLatestPrice() {
        if (MainClass.INSTANCE.getConfig().contains("latestPrice")) {
            return new BigDecimal(MainClass.INSTANCE.getConfig().getString("latestPrice"));
        }
        return new BigDecimal(10);
    }

    public static void saveLatestPrice(BigDecimal price) {
        MainClass.INSTANCE.getConfig().set("latestPrice",price.toString());
        MainClass.INSTANCE.saveConfig();
    }

    public static void saveLastDayPrice( BigDecimal price) {
        MainClass.INSTANCE.getConfig().set("lastDayPrice",price.toString());
        MainClass.INSTANCE.saveConfig();
    }

    public static BigDecimal getLastDayPrice() {
        if (MainClass.INSTANCE.getConfig().contains("lastDayPrice")) {
            return new BigDecimal(MainClass.INSTANCE.getConfig().getString("lastDayPrice"));
        }
        return new BigDecimal(10);
    }

    public static BigInteger getLastDayVolume() {
        if (MainClass.INSTANCE.getConfig().contains("lastDayVolume")) {
            return new BigInteger(MainClass.INSTANCE.getConfig().getString("lastDayVolume"));
        }
        return BigInteger.ZERO;
    }

    public static void setLastDayVolume(BigInteger lastDayVolume) {
        MainClass.INSTANCE.getConfig().set("lastDayVolume",lastDayVolume.toString());
        MainClass.INSTANCE.saveConfig();
    }

    public static BigInteger getTodayVolume() {
        if (MainClass.INSTANCE.getConfig().contains("todayVolume")) {
            return new BigInteger(MainClass.INSTANCE.getConfig().getString("todayVolume"));
        }
        return BigInteger.ZERO;
    }

    public static void setTodayVolume(BigInteger todayVolume) {
        MainClass.INSTANCE.getConfig().set("todayVolume",todayVolume.toString());
        MainClass.INSTANCE.saveConfig();
    }

    public static BigDecimal getLimit() {
        return new BigDecimal(MainClass.INSTANCE.getConfig().getString("limitRate"));
    }

}
